package com.tianyang.yukuaifu.business.auth.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.facebook.drawee.view.SimpleDraweeView;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseEditActivity;
import com.tianyang.yukuaifu.business.auth.bean.AuthServiceInput;
import com.tianyang.yukuaifu.business.auth.bean.BankRegOutput;
import com.tianyang.yukuaifu.business.auth.bean.BusinessOutput;
import com.tianyang.yukuaifu.business.auth.bean.IdCardOutput;
import com.tianyang.yukuaifu.business.auth.contract.AuthConfig;
import com.tianyang.yukuaifu.business.auth.contract.AuthServiceContract;
import com.tianyang.yukuaifu.business.auth.presenter.AuthServicePresenter;
import com.tianyang.yukuaifu.business.auth.provider.AuthOpenProvider;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.utils.SoftInputUtil;
import com.tianyang.yukuaifu.utils.ToastUtil;
import com.tianyang.yukuaifu.utils.VerifyUtil;
import com.tianyang.yukuaifu.utils.permissions.PermissionsConstant;
import com.tianyang.yukuaifu.utils.permissions.PermissionsMgr;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：服务商实名认证
 * Created by xyj on 2024/3/22
 */
public class AuthServiceActivity extends BaseEditActivity implements AuthServiceContract.View {

    private String[] permissions = new String[]{PermissionsConstant.CAMERA, PermissionsConstant.WRITE_STORAGE, PermissionsConstant.READ_STORAGE};

    @BindView(R.id.idCardFrontImgSdv)
    SimpleDraweeView idCardFrontImgSdv;
    @BindView(R.id.idCardBackImgSdv)
    SimpleDraweeView idCardBackImgSdv;
    @BindView(R.id.bankFrontImgSdv)
    SimpleDraweeView bankFrontImgSdv;
    @BindView(R.id.xinmingEt)
    EditText xinmingEt;
    @BindView(R.id.shenfengidEt)
    EditText shenfengidEt;
    @BindView(R.id.bankcardidEt)
    EditText bankcardidEt;
    @BindView(R.id.bankTelEt)
    EditText bankTelEt;
    @BindView(R.id.openingBankTv)
    TextView openingBankTv;

    private AuthServiceContract.Presenter mPresenter;

    private String currentPhotoType;
    
    private AuthServiceInput mAuthServiceInput;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_auth_service;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("实名认证");
        mPresenter = new AuthServicePresenter(this);
        mAuthServiceInput = new AuthServiceInput();
    }

    @OnClick({R.id.confirmTv, R.id.idCardFrontImgRfl, R.id.idCardBackImgRfl, R.id.bankFrontImgRfl, R.id.openingBankTv,
            R.id.takeIdCardFrontTv, R.id.takeIdCardBackTv, R.id.takeBankFrontTv})
    public void onClick(View view) {
        if (!isRespondEvent() || getContext() == null) return;
        switch (view.getId()) {
            case R.id.confirmTv:
                if (checkServiceInput()) {
                    SoftInputUtil.hideSoftInput(this);
                    Map<String, String> input = JSON.parseObject(JSONObject.toJSONString(mAuthServiceInput),
                            new TypeReference<Map<String, String>>(){});
                    mPresenter.sendIdCert(input);
                }
                break;
            case R.id.idCardFrontImgRfl:
                currentPhotoType = CommonConfig.TYPE_IDFROPIC;
                if (!PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
                    requestPermissions(permissions);
                    return;
                }
                mPresenter.showSelectImgSourceView(currentPhotoType);
                break;
            case R.id.idCardBackImgRfl:
                currentPhotoType = CommonConfig.TYPE_IDCONPIC;
                if (!PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
                    requestPermissions(permissions);
                    return;
                }
                mPresenter.showSelectImgSourceView(currentPhotoType);
                break;
            case R.id.bankFrontImgRfl:
                currentPhotoType = CommonConfig.TYPE_CRDFROPIC;
                if (!PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
                    requestPermissions(permissions);
                    return;
                }
                mPresenter.showSelectImgSourceView(currentPhotoType);
                break;
            case R.id.openingBankTv:
                AuthOpenProvider.getInstance().openBusinessInfoActivity(mContext, AuthConfig.MCH_TYPE_SMALL,
                        AuthConfig.MCC_TYPE_BANK, "", CommonConfig.REQUESTCODE_AUTH_BUSINESS_BANK);
                break;
            case R.id.takeIdCardFrontTv:
                ToastUtil.show("查看身份证人像面示例");
                break;
            case R.id.takeIdCardBackTv:
                ToastUtil.show("查看身份证国徽面示例");
                break;
            case R.id.takeBankFrontTv:
                ToastUtil.show("查看银行卡卡号照示例");
                break;
        }
    }

    @Override
    public void showPhotoView(String urlStr, String photoType) {
        switch (photoType) {
            case CommonConfig.TYPE_IDFROPIC:
                idCardFrontImgSdv.setImageURI(Uri.parse(urlStr));
                mAuthServiceInput.setShenfengzhengpic(urlStr);
                break;
            case CommonConfig.TYPE_IDCONPIC:
                idCardBackImgSdv.setImageURI(Uri.parse(urlStr));
                mAuthServiceInput.setShenfengfanpic(urlStr);
                break;
            case CommonConfig.TYPE_CRDFROPIC:
                bankFrontImgSdv.setImageURI(Uri.parse(urlStr));
                mAuthServiceInput.setBankcardpic(urlStr);
                break;
        }
    }

    @Override
    public void showIdCardView(IdCardOutput output) {
        if (output != null) {
            if (TextUtils.equals("1", output.getCodeType())) {
                xinmingEt.setText(VerifyUtil.checkString(output.getName()));
                mAuthServiceInput.setXinming(output.getName());
                shenfengidEt.setText(VerifyUtil.checkString(output.getCode()));
                mAuthServiceInput.setShenfengid(output.getCode());
            } 
        }
    }

    @Override
    public void showBankCardView(BankRegOutput output) {
        if (output != null) {
            if (!TextUtils.isEmpty(output.getBank_card_number())) {
                String bankCardId = output.getBank_card_number().replaceAll(" ", "");
                bankcardidEt.setText(bankCardId);
                mAuthServiceInput.setBankcardid(bankCardId);
            }
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CommonConfig.REQUESTCODE_AUTH_BUSINESS_BANK) {
            if (resultCode == Activity.RESULT_OK && data != null) {
                BusinessOutput output = (BusinessOutput) data.getSerializableExtra(AuthConfig.INTENT_BUSINESS_INFO);
                if (output != null) {
                    openingBankTv.setText(VerifyUtil.checkString(output.getBranchBankName()));
                    mAuthServiceInput.setOpeningBank(output.getBranchBankName());
                    mAuthServiceInput.setBranchBankNo(output.getBranchNo());
                }
            }
        } else {
            mPresenter.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onPermissionGranted(List<String> permission) {
        if (PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
            mPresenter.showSelectImgSourceView(currentPhotoType);
        }
    }

    public boolean checkServiceInput() {
        if (TextUtils.isEmpty(mAuthServiceInput.getShenfengzhengpic())) {
            ToastUtil.show("请上传身份证人像面");
            return false;
        }
        if (TextUtils.isEmpty(mAuthServiceInput.getShenfengfanpic())) {
            ToastUtil.show("请上传身份证国徽面");
            return false;
        }
        if (TextUtils.isEmpty(mAuthServiceInput.getBankcardpic())) {
            ToastUtil.show("请上传银行卡卡号照");
            return false;
        }
        if (TextUtils.isEmpty(xinmingEt.getText())) {
            ToastUtil.show("请输入姓名");
            SoftInputUtil.openKeybord(xinmingEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(shenfengidEt.getText())) {
            ToastUtil.show("请输入身份证号");
            SoftInputUtil.openKeybord(shenfengidEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(bankcardidEt.getText())) {
            ToastUtil.show("请填写银行卡号");
            SoftInputUtil.openKeybord(bankcardidEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(bankTelEt.getText())) {
            ToastUtil.show("请填写预留手机号");
            SoftInputUtil.openKeybord(bankTelEt, getContext());
            return false;
        } else if (VerifyUtil.checkPhoneNumber(bankTelEt.getText())) {
            ToastUtil.show("请输入正确的手机号");
            SoftInputUtil.openKeybord(bankTelEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(mAuthServiceInput.getOpeningBank())) {
            ToastUtil.show("请选择开户支行");
            return false;
        }
        mAuthServiceInput.setXinming(xinmingEt.getText().toString());
        mAuthServiceInput.setShenfengid(shenfengidEt.getText().toString());
        mAuthServiceInput.setBankcardid(bankcardidEt.getText().toString());
        mAuthServiceInput.setBankTel(bankTelEt.getText().toString());
        return true;
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(AuthServiceContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
     }
}
